<?php

include_once dirname(dirname(dirname(__FILE__))) . '/shared/db_connection.php';

function getAllPapersByJournalIDnDated($journal_id, $month, $year) {
    $con = open_connect();
    //run the store proc
    $result = mysqli_query($con, "CALL GetAllPapersByJournalIDnDated(".$journal_id.",'".$month."','".$year."')") 
            or die("Query fail: " . mysqli_error());
    
    $list = array();
    $count = 0;
    while($row = mysqli_fetch_array($result))
    {
            $item = array();
            $item["PaperID"] = $row['PaperID'];
            $item["Title"] = $row['Title'];
            $item["Author"] = $row['Author'];
            $item["Citation"] = $row['Citation'];
            $item["Keywords"] = $row['Keywords'];
            $item["ArticleType"] = $row['ArticleType'];
            $item["Pages"] = $row['Pages'];
            $item["DOI"] = $row['DOI'];
            $item["Publisher"] = $row['Publisher'];
            $item["Acknowlegdements"] = $row['Acknowlegdements'];
            $item["Abstract"] = $row['Abstract'];
            $item["PDFFiles"] = $row['PDFFiles'];
            $item["Dated"] = $row['Dated'];
            $item["DateByUser"] = $row['DateByUser'];
            $item["JournalID"] = $row['JournalID'];
            $item["journalTitle"] = $row['journalTitle'];
            $list[$count] = $item;
            $count = $count + 1;
    }
    
    close_connect($con);
    return $list;
}

function getAllPapersByJournalID($journal_id) {
    $con = open_connect();
    //run the store proc
    $result = mysqli_query($con, "SELECT * FROM tblPapers WHERE JournalID = ".$journal_id." ORDER BY DateByUser DESC") 
            or die("Query fail: " . mysqli_error());
    
    $list = array();
    $count = 0;
    while($row = mysqli_fetch_array($result))
    {
            $item = array();
            $item["PaperID"] = $row['PaperID'];
            $item["Title"] = $row['Title'];
            $item["Author"] = $row['Author'];
            $item["Citation"] = $row['Citation'];
            $item["Keywords"] = $row['Keywords'];
            $item["ArticleType"] = $row['ArticleType'];
            $item["Pages"] = $row['Pages'];
            $item["DOI"] = $row['DOI'];
            $item["Publisher"] = $row['Publisher'];
            $item["Acknowlegdements"] = $row['Acknowlegdements'];
            $item["Abstract"] = $row['Abstract'];
            $item["PDFFiles"] = $row['PDFFiles'];
            $item["Dated"] = $row['Dated'];
            $item["DateByUser"] = $row['DateByUser'];
            $item["JournalID"] = $row['JournalID'];
            $item["journalTitle"] = $row['journalTitle'];
            $list[$count] = $item;
            $count = $count + 1;
    }
    
    close_connect($con);
    return $list;
}

function getPapersById($paper_id) {
    $con = open_connect();
    //run the store proc
    $result = mysqli_query($con, "CALL GetPapersById(".$paper_id.")") 
            or die("Query fail: " . mysqli_error());
    

    $row = mysqli_fetch_array($result);
    $item = array();
    $item["PaperID"] = $row['PaperID'];
    $item["Title"] = $row['Title'];
    $item["Author"] = $row['Author'];
    $item["Citation"] = $row['Citation'];
    $item["Keywords"] = $row['Keywords'];
    $item["ArticleType"] = $row['ArticleType'];
    $item["Pages"] = $row['Pages'];
    $item["DOI"] = $row['DOI'];
    $item["Publisher"] = $row['Publisher'];
    $item["Acknowlegdements"] = $row['Acknowlegdements'];
    $item["Abstract"] = $row['Abstract'];
    $item["PDFFiles"] = $row['PDFFiles'];
    $item["Dated"] = $row['Dated'];
    $item["DateByUser"] = $row['DateByUser'];
    $item["JournalID"] = $row['JournalID'];
    $item["journalTitle"] = $row['journalTitle'];
    
    close_connect($con);
    return $item;
}